package com.aizhixin.lab.company.service;

import com.aizhixin.lab.common.utils.PageJdbcUtil;
import com.aizhixin.lab.project.course.domain.StuTaskListDomain;
import com.aizhixin.lab.project.course.uilt.NumToChinese;
import lombok.extern.slf4j.Slf4j;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.aizhixin.lab.common.core.ApiReturnConstants;
import com.aizhixin.lab.common.core.DataValidity;
import com.aizhixin.lab.company.domain.OrganDomain;
import com.aizhixin.lab.company.domain.SystemSetDomain;
import com.aizhixin.lab.company.entity.College;
import com.aizhixin.lab.company.entity.Professional;
import com.aizhixin.lab.company.entity.SystemSet;
import com.aizhixin.lab.company.entity.User;
import com.aizhixin.lab.company.repository.ProfessionalRepository;
import com.aizhixin.lab.company.repository.SystemSetRepository;

@Slf4j
@Service
@Transactional
public class ProfessionalService {
	@Autowired
	private ProfessionalRepository professionalRepository;
    @Autowired
    private PageJdbcUtil pageJdbcUtil;
	public Map<String, Object> save(OrganDomain domain) {
		Map<String, Object> result = new HashMap<>();
		Professional professional = null;
		if (null != domain.getId() && domain.getId() > 0) {
			professional = professionalRepository.findOne(domain.getId());
			professional.setName(domain.getName());
			professional.setDescription(domain.getDescription());
			professional.setCollegeId(domain.getCollegeId());
		}else{
			professional = new Professional();
			BeanUtils.copyProperties(domain, professional);
		}
		professionalRepository.save(professional);
		result.put(ApiReturnConstants.SUCCESS, true);

		return result;
	}
	RowMapper<Professional> rm = new RowMapper<Professional>() {

		@Override
		public Professional mapRow(ResultSet rs, int rowNum)
				throws SQLException {
			// TODO Auto-generated method stub
			Professional domain = new Professional();
			domain.setId(rs.getLong("id"));
			domain.setName(rs.getString(2));
			domain.setDescription(rs.getString(3));
			domain.setCollegeId(rs.getLong(4));
			return domain;
		}
	};
	public List<Professional> getAllProfessional(Long collegeId){
		if (collegeId==null){
			return professionalRepository.findAllByDeleteFlagOrderById(DataValidity.VALID.getState());
		}
        pageJdbcUtil.getInfo("SELECT * FROM t_professional WHERE DELETE_FLAG=0 AND COLLEGE_ID in (2,"+collegeId+")  GROUP BY `NAME` ORDER BY COLLEGE_ID ASC ,id ASC",rm);
		return professionalRepository.findAllByCollegeIdAndDeleteFlagOrderById(collegeId,DataValidity.VALID.getState());
	}
	public void save(List<Professional> professionalList){
		professionalRepository.save(professionalList);
	}

	public Map<String, Object> getAllByCollegeId(Long collegeId) {
		Map<String, Object> result = new HashMap<>();
		List<Professional> professionalList = professionalRepository.findAllByCollegeIdAndDeleteFlagOrderById(collegeId, DataValidity.VALID.getState());
		if (!professionalList.isEmpty()) {
			result.put(ApiReturnConstants.DATA, professionalList);
		}
		result.put(ApiReturnConstants.SUCCESS, true);
		return result;
	}
	
	
	public Map<String, Object> getAll() {
		Map<String, Object> result = new HashMap<>();
		List<Professional> professionalList = professionalRepository.findAllByDeleteFlagOrderById(DataValidity.VALID.getState());
		if (!professionalList.isEmpty()) {
			result.put(ApiReturnConstants.DATA, professionalList);
		}
		
		result.put(ApiReturnConstants.SUCCESS, true);

		return result;
	}
	
	
	public Map<String, Object> delete(OrganDomain domain) {
		Map<String, Object> result = new HashMap<>();
		Professional professional = professionalRepository.findOneByIdAndDeleteFlag(domain.getId(), DataValidity.VALID.getState());
		if (null != professional ) {
			professional.setDeleteFlag(DataValidity.INVALID.getState());
			professionalRepository.save(professional);
			result.put(ApiReturnConstants.SUCCESS, true);
		}else{
			result.put(ApiReturnConstants.SUCCESS, false);
			result.put(ApiReturnConstants.ERROR, "专业信息不存在");
		}

		return result;
	}
}
